package dataProviders;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

import wrappers.Entity;

public class EntityComboBox implements ComboBoxModel{
	Set<ListDataListener> ldl = new HashSet<ListDataListener>();
	ArrayList<Entity> entities = new ArrayList<Entity>();
	Object selected = null;
	
	public void setEntities(ArrayList<Entity> input){
		entities = input;
	}
	
	@Override
	public void addListDataListener(ListDataListener arg0) {
		ldl.add(arg0);
		
	}

	@Override
	public Object getElementAt(int arg0) {
		return ((Entity)entities.toArray()[arg0]).getName();
	}

	@Override
	public int getSize() {
		return entities.size();
	}

	@Override
	public void removeListDataListener(ListDataListener arg0) {
		ldl.remove(arg0);		
	}

	@Override
	public Object getSelectedItem() {
		return selected;
	}

	@Override
	public void setSelectedItem(Object arg0) {
		selected = arg0;
		
	}
	
	public Object findItem(int id){
		for (Entity it : entities) {
			if(it.getId() == id)
				return it;
		}
		return null;
	}
	
	public int getSelectedId(){
		for (Entity it : entities) {
			if(it.getName().equals(selected.toString())){
				System.out.println(it.getId()+" "+it.getName());
				return it.getId();
			}
		}
		return -1;
	}
	
	public int getSelectedId(int index){
		return ((Entity)entities.toArray()[index]).getId();
	}

}
